package data.goods;

import java.util.ArrayList;

import data.ListInterIterator;

public class Stockfind {
	private ArrayList<Goods> list;
	private ArrayList<GoodsType> templist=new ArrayList<GoodsType>();
	private ArrayList<Goods> find=new ArrayList<Goods>();
	private ArrayList<GoodsType> tempfind=new ArrayList<GoodsType>();
	
	public Stockfind (ListInterIterator<Goods> list){
		this.list=list.toArray();
	}
	
	public ListInterIterator<Goods> find(String key,String message){
		if(key.equals("startdate")){
			findbyStartdate(message);
		}
		if(key.equals("enddate")){
			findbyEnddate(message);
		}
		else {
			change();
			GoodsTypefind one=new GoodsTypefind(new ListInterIterator<GoodsType>(templist));
			tempfind=one.find(key, message).toArray();
			changeTo();
		}
		return new ListInterIterator<Goods>(find);
	}
	
	private void findbyStartdate(String message){
		for(int i=0;i<list.size();i++){
			if(list.get(i).getProductionDate().equals(message)){
				find.add(list.get(i));
			}
		}
	}
	
	private void findbyEnddate(String message){
		for(int i=0;i<list.size();i++){
			if(list.get(i).getScrapDate().equals(message)){
				find.add(list.get(i));
			}
		}
	}
	
    private void change(){
    	for(int i=0;i<list.size();i++){
    		templist.add((GoodsType) (new GoodsTypeList()).idFind(list.get(i).getID()));
    	}	
    }
    
    private void changeTo(){
    	for(int i=0;i<list.size();i++){
    		find.add((Goods) (new StockList()).idFind(tempfind.get(i).getID()));
    	}	
    }
}
